package com.smartlink.action;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.smartlink.service.SignInService;

/**
 * 在线签到
 * 
 * @author Administrator
 *
 */
public class SignInAction extends ActionSupport implements RequestAware, ServletResponseAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@SuppressWarnings("unused")
	private Map<String, Object> request;
	private HttpServletResponse response;
	private String number;
	private int classroomId;
	private SignInService signInService;

	public void setNumber(String number) {
		this.number = number;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;

	}

	@Override
	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;

	}

	public int getClassroomId() {
		return classroomId;
	}

	public void setClassroomId(int classroomId) {
		this.classroomId = classroomId;
	}

	public void setSignInService(SignInService signInService) {
		this.signInService = signInService;
	}

	/**
	 * 学生签到
	 * 
	 * @param studentId
	 * @throws Exception
	 */
	public void signIn() throws Exception {
		PrintWriter out = response.getWriter();
		if (!isOnLine(number)) {
			// 不在线，可以签到
			// 同步锁，app在调用位置改变是会很快调用该方法，防止重复签到
			synchronized (SignInAction.class) {
				int result = signInService.signIn(number, classroomId);
				String json = null;
				if (result != -1) {
					json = "{\"result\":\"" + "success" + "\"}";
				} else {
					json = "{\"result\":\"" + "wrong" + "\"}";
				}
				out.println(json);
				out.close();
			}

		} else {
			// 在线不能签到
			String result = "haveSigin";
			String json = "{\"classroomId\":\"" + result + "\"}";
			out.println(json);
			out.close();
		}
	}

	public void isSignin() throws Exception {
		PrintWriter out = response.getWriter();
		boolean flag = isOnLine(number);
		JSONObject json = new JSONObject();
		json.put("result", flag);
		out.println(json);
		out.close();

	}

	/**
	 * 判断学生是否在线（签到过）
	 * 
	 * @param studentId
	 * @return
	 */
	private boolean isOnLine(String studentId) {

		return signInService.isOnLine(studentId);
	}

	/**
	 * 判断学生是否存在 result:true 存在
	 * 
	 * @return
	 * @throws Exception
	 */
	public void isStudentExist() throws Exception {
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		String json = signInService.isStudentExist(number);
		out.println(json);
		out.close();

	}

	/**
	 * 获取签到统计信息，包括总人数，签到人数，未签到人员信息
	 * 
	 * @throws Exception
	 */
	public void getSignInInfo() throws Exception {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter out = response.getWriter();
		String info = signInService.getSiginInfos(classroomId);
		System.out.println(info);
		out.println(info);
		out.close();
	}

	public void setStudentFace() throws Exception {
		PrintWriter out = response.getWriter();
		boolean flag = signInService.setStudentFace(number);
		JSONObject json = new JSONObject();
		json.put("resule", flag);
		out.println(json);
		out.close();

	}

}
